Create PROCEDURE [dbo].[spGetUpdateableDrug]
    @WearhouseCode VARCHAR(10),
    @ComputerName NVARCHAR(MAX),
    @CodeType SMALLINT,
    @IsOrgType BIT,
    @OrgCode VARCHAR(MAX),
    @UpdateType SMALLINT,
    @JustSelect BIT
AS
IF @JustSelect = 0
BEGIN
    IF OBJECT_ID('tempdb..##tmpView') IS NOT NULL
        DROP TABLE tempdb..##tmpView;
    DECLARE @cmd NVARCHAR(MAX),
            @field NVARCHAR(MAX),
            @join NVARCHAR(MAX),
            @orderBy NVARCHAR(MAX);

    IF @CodeType = 2
    BEGIN
        SET @join = N' Inner join KalaId G ON G.K_Code COLLATE  Arabic_CI_AS = tmp.K_Code ';
        SET @orderBy = N' Order By tmp.K_Code  ';
    END;
    IF @CodeType = 1
    BEGIN
        SET @join = N' Inner join KalaId G ON G.Brand_Code COLLATE  Arabic_CI_AS = tmp.Brand_Code ';
        SET @orderBy = N' Order By tmp.Brand_Code  ';
    END;

    IF @CodeType = 3
    BEGIN
        SET @join = N' Inner join KalaId G ON G.Org_Code COLLATE  Arabic_CI_AS = tmp.Org_Code ';
        SET @orderBy = N' Order By tmp.Org_Code  ';
    END;
    IF @CodeType = 0
    BEGIN
        SET @join = N' Inner join KalaId G ON G.Meli_Code COLLATE  Arabic_CI_AS = tmp.M_code ';
        SET @orderBy = N' Order By tmp.M_code ';
    END;
    IF @CodeType = 4
        SET @orderBy = N' Order By G.M_code ';

    IF @CodeType <> 4
    BEGIN
        IF @IsOrgType = 1
        BEGIN
            SET @cmd = N' G.Darou_Flag = ''6'' AND PO.Sazman_Code ' + @OrgCode;

            SET @field
                = N' , PO.Price AS sa_Price, PO.Status ,ISNULL(PO.[BimarPercent],0) CurPercent , PO.Sazman_Code';
            SET @join
                = @join
                  + N' INNER JOIN  Price_Sazman PO ON G.K_Code = PO.K_Code AND PO.Status NOT in (0 , 1 , 2 , 3 ) AND '
                  + @cmd;
        END;
        ELSE
            SET @field
                = N' , Anbar.Price_Kharid AS sa_Price, Darou_Flag AS Status , 0 as CurPercent , ''01'' as Sazman_Code ';
        SET @cmd
            = N'SELECT DISTINCT tmp.Name ,tmp.M_Code ,tmp.Price ,G.Name1 ,G.K_Code ,'
              + N'Anbar.Price_Forosh AS Old_Price ,G.Darou_Flag ' + @field + N' , tmp.Update_Status ,'
              + N'tmp.K_Code tmpKCode ,st_Name = CASE WHEN tmp.Update_Status = 1 THEN N'' '''
              + N'ELSE N'' '' END , tmp.Computer_Name ,tmp.Condition ,tmp.[Percent] ,'
              + N'tmp.Brand_Code , tmp.Org_Code, 0 isReduce ' + N'INTO  ##tmpView ' + N'FROM  ##tmpExcel tmp  ' + @join
              + N'Inner Join Anbar  ON Anbar.K_Code     = G.K_Code ' + N'WHERE tmp.Computer_Name = ' + @ComputerName
              + N' AND Anbar.A_Code = ' + @WearhouseCode + N'';

        IF @UpdateType = 2
            SET @cmd = @cmd + N' AND Anbar.Price_Forosh <> tmp.Price AND G.Darou_Flag <> ''6'' ';
        ELSE IF @UpdateType = 1
            SET @cmd = @cmd + N' AND Anbar.Price_Forosh = tmp.Price AND G.Darou_Flag <> ''6'' ';
        ELSE IF @UpdateType = 3
        BEGIN
            SET @cmd
                = @cmd
                  + N' AND ( ((PO.Status = ''6'' AND tmp.Price > PO.Price ) OR (PO.Status not in ( ''4'' , ''6'' , ''9'' ) AND tmp.Price > Anbar.Price_Forosh))'
                  + N' OR  (PO.Status in (''4'' , ''9'') AND (( PO.Price > 0 AND tmp.Price > PO.Price ) OR (PO.Price = 0 AND tmp.Price > Anbar.Price_Forosh ))) '
                  + N' OR ((PO.Status = ''6'' AND tmp.Price < PO.Price ) OR (PO.Status not in (''4'' , ''6'' , ''9'' ) AND tmp.Price < Anbar.Price_Forosh))'
                  + N' OR (PO.Status in (''4'' , ''9'') AND ( ( PO.Price > 0 AND tmp.Price < PO.Price ) OR (PO.Price = 0 AND tmp.Price < Anbar.Price_Forosh))) )'
                  + N' AND NOT( PO.Status = ''6'' AND Anbar.Price_Forosh = tmp.Price AND ISNULL(PO.BimarPercent , 0) > 0 AND PO.Price = 0 ) ';
        END;
    END;
    ELSE
    BEGIN
        IF @IsOrgType = 1
        BEGIN
            SET @cmd = N' G.Darou_Flag = ''6'' AND PO.Sazman_Code = G.InsuranceCode '-- + @OrgCode;
            SET @field
                = N' , PO.Price AS sa_Price, PO.Status ,ISNULL(PO.[BimarPercent],0) CurPercent , PO.Sazman_Code';
            SET @join
                = N' INNER JOIN  Price_Sazman PO ON G.K_Code = PO.K_Code AND PO.Status NOT in (0 , 1 , 2 , 3 ) AND '
                  + @cmd;
        END;
        ELSE
            SET @field
                = N' , Anbar.Price_Kharid AS sa_Price, Darou_Flag AS Status , 0 as CurPercent , ''01'' as Sazman_Code ';
        SET @cmd
            = N'SELECT G.K_Code,G.Name1,G.Name,G.M_Code,G.Darou_Flag,G.Price,G.Update_Status,st_Name = CASE WHEN G.Update_Status = 1 THEN N'' '' ELSE N'' '' END,'
              + N'G.Computer_Name,G.Condition,G.[Percent],G.Brand_Code,G.Org_Code,0 isReduce,Anbar.Price_Forosh AS Old_Price '
              + @field + N' INTO  ##tmpView FROM' + N'('
              + N'SELECT g.K_Code,g.Name1,g.Darou_Flag,EIC.Code M_Code,tmp.Brand_Code,tmp.Org_Code,tmp.Price,tmp.Update_Status,tmp.Computer_Name,tmp.Condition,tmp.[PERCENT],tmp.Name , InsuranceCode , ''Exclusive'' [Type] '
              + N'FROM ##tmpExcel tmp '
              + N'JOIN dbo.ExclusiveInsuranceCode EIC ON EIC.Code = tmp.ExclusiveCode AND EIC.InsuranceCode '
              + @OrgCode + N'JOIN dbo.KalaId g ON g.K_Code = EIC.GoodsCode ' + N'UNION ALL '
              + N'SELECT g.K_Code, g.Name1,g.Darou_Flag,g.Meli_Code M_Code ,tmp.Brand_Code,tmp.Org_Code,tmp.Price,tmp.Update_Status,tmp.Computer_Name,tmp.Condition,tmp.[PERCENT],tmp.Name , ''0'' as InsuranceCode  , ''MCode'' [Type] '
              + N'FROM ##tmpExcel tmp ' + N'JOIN dbo.KalaId g ON g.Meli_Code = tmp.M_Code ' + N'WHERE g.K_Code NOT IN '
              + N'(' + N'SELECT g.K_Code ' + N'FROM ##tmpExcel tmp '
              + +N'JOIN dbo.ExclusiveInsuranceCode EIC ON EIC.Code = tmp.ExclusiveCode AND EIC.InsuranceCode '
              + @OrgCode + N'JOIN dbo.KalaId g ON g.K_Code = EIC.GoodsCode ' + N')' + N') AS G ' + @join
              + N'JOIN dbo.Anbar ' + N'ON dbo.Anbar.K_Code = G.K_Code ' + N'WHERE G.Computer_Name = ' + @ComputerName
              + N' AND Anbar.A_Code = ' + @WearhouseCode + N'';
        IF @UpdateType = 2
            SET @cmd = @cmd + N' AND Anbar.Price_Forosh <> G.Price AND G.Darou_Flag <> ''6'' ';
        ELSE IF @UpdateType = 1
            SET @cmd = @cmd + N' AND Anbar.Price_Forosh = G.Price AND G.Darou_Flag <> ''6'' ';
        ELSE IF @UpdateType = 3
        BEGIN
            SET @cmd
                = @cmd
                  + N' AND ( ((PO.Status = ''6'' AND G.Price > PO.Price ) OR (PO.Status not in ( ''4'' , ''6'' , ''9'' ) AND G.Price > Anbar.Price_Forosh))'
                  + N' OR  (PO.Status in (''4'' , ''9'') AND (( PO.Price > 0 AND G.Price > PO.Price ) OR (PO.Price = 0 AND G.Price > Anbar.Price_Forosh ))) '
                  + N' OR ((PO.Status = ''6'' AND G.Price < PO.Price ) OR (PO.Status not in (''4'' , ''6'' , ''9'' ) AND G.Price < Anbar.Price_Forosh))'
                  + N' OR (PO.Status in (''4'' , ''9'') AND ( ( PO.Price > 0 AND G.Price < PO.Price ) OR (PO.Price = 0 AND G.Price < Anbar.Price_Forosh))) )'
                  + N' AND NOT( PO.Status = ''6'' AND Anbar.Price_Forosh = G.Price AND ISNULL(PO.BimarPercent , 0) > 0 AND PO.Price = 0 ) ';
        END;
    END;
    SET @cmd = @cmd + @orderBy;
    PRINT '--->' + @cmd;
    EXEC sp_executesql @cmd;

END;
UPDATE ##tmpView
SET isReduce = CASE
                   WHEN
                   (
                       Price < Old_Price
                       AND
                       (
                           Price < sa_Price
                           OR
                           (
                               sa_Price > 0
                               OR Status IN ( 4, 5, 9 )
                           )
                       )
                   ) THEN
                       1
                   ELSE
                       0
               END;
SELECT [Name],
       M_Code,
       Price,
       Name1,
       K_Code,
       Old_Price,
       Darou_Flag,
       sa_Price,
       Status,
       Update_Status,
       st_Name,
       Condition,
       Computer_name,
       CAST([Percent] AS REAL) [Percent],
       CAST(CurPercent AS REAL) curPercent,
       Status_Name = CASE
                         WHEN Darou_Flag <> 6 THEN
                             dbo.Find_Status(Darou_Flag)
                         ELSE
                             dbo.Find_Status(Status)
                     END,
       Brand_Code,
       Org_Code,
       Sazman_Code,
       isReduce
FROM ##tmpView;
